unit UAssignItemInsurancePricing;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, AdvPanel, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxStyles, dxSkinsCore,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, dxSkinscxPCPainter, cxCustomData,
  cxFilter, cxData, cxDataStorage, cxEdit, cxNavigator, Data.DB, cxDBData,
  cxGridLevel, cxClasses, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, DBAdvGlowNavigator, cxContainer,
  cxGroupBox, UDMItems, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit,
  cxDBLookupEdit, cxDBLookupComboBox, Vcl.StdCtrls, UDMPerson, cxDBEdit,
  Vcl.Menus, cxButtons;

type
  TFAssignItemInusrancePricing = class(TForm)
    AdvPanel1: TAdvPanel;
    DBAdvGlowNavigator1: TDBAdvGlowNavigator;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    AdvPanelStyler1: TAdvPanelStyler;
    cxGroupBox1: TcxGroupBox;
    AdvPanel2: TAdvPanel;
    Label2: TLabel;
    Label3: TLabel;
    cxGrid1DBTableView1coef_id: TcxGridDBColumn;
    cxGrid1DBTableView1currency_id: TcxGridDBColumn;
    cxGrid1DBTableView1price: TcxGridDBColumn;
    cxLookupComboBox1: TcxLookupComboBox;
    cxLookupComboBox2: TcxLookupComboBox;
    cxButton1: TcxButton;
    procedure cxButton1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FAssignItemInusrancePricing: TFAssignItemInusrancePricing;

implementation

{$R *.dfm}

procedure TFAssignItemInusrancePricing.cxButton1Click(Sender: TObject);
var coefCount : Integer;
var i : Integer;
var insuranceID : Integer;
var coefID : Integer;
var currencyID : Integer;
begin
  if (Self.cxLookupComboBox1.EditValue = Null) or (Self.cxLookupComboBox2.EditValue = Null)then
  begin
    ShowMessage('Please choose the Insurance and the Currency first');
  end
  else
  begin
    DMItems.his_item_insurance_pricing.Active := False;
    DMItems.his_item_insurance_pricing.ParamByName('a').Value := Self.cxLookupComboBox1.EditValue;
    DMItems.his_item_insurance_pricing.ParamByName('b').Value := Self.cxLookupComboBox2.EditValue;
    DMItems.his_item_insurance_pricing.Execute;
    DMItems.his_item_insurance_pricing.Active := False;
    DMItems.his_item_insurance_pricing.Active := True;

    coefCount := DMItems.his_coef.RecordCount;
    insuranceID := Self.cxLookupComboBox1.EditValue;
    currencyID := Self.cxLookupComboBox2.EditValue;
    DMItems.his_coef.First;
    for i := 0 to coefCount-1 do
    begin
      coefID := DMItems.his_coef.FieldByName('coef_id').Value;

      IF DMItems.his_item_insurance_pricing_table.Locate('insurance_id;coef_id;currency_id',VarArrayOf([insuranceID,coefID,currencyID]), [loPartialKey]) = False THEN
      BEGIN
        IF DMItems.his_item_insurance_pricing_table.Locate('insurance_id;coef_id',VarArrayOf([insuranceID,coefID]), []) = False THEN
        BEGIN
          DMItems.his_item_insurance_pricing.Insert;
          DMItems.his_item_insurance_pricing.FieldByName('insurance_id').Value := insuranceID;
          DMItems.his_item_insurance_pricing.FieldByName('coef_id').Value := coefID;
          DMItems.his_item_insurance_pricing.FieldByName('currency_id').Value := currencyID;
          DMItems.his_item_insurance_pricing.Post;
          DMItems.his_item_insurance_pricing.RefreshRecord;
        END;
      END;
      DMItems.his_item_insurance_pricing.Next;
      DMItems.his_coef.Next;
    end;
    DMItems.his_insurance_coef_list.Refresh;
    DMItems.his_item_insurance_pricing_table.Refresh;
  end;
  Self.DBAdvGlowNavigator1.Enabled := True;
end;


procedure TFAssignItemInusrancePricing.FormActivate(Sender: TObject);
begin
  DMItems.combine_tpa_class.Execute;

  DMItems.his_tpa_class_combine_for_pricing_group.Refresh;
  DMItems.his_currency.Refresh;
end;

end.
